package com.sisyphus.simple.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sisyphus.simple.data.DataRepository;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.core.NetTaskFactory;
import com.sisyphus.spinach.network.presenters.FormTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.utils.Form;
import com.sisyphus.spinach.network.views.Debounce;
import com.sisyphus.spinach.network.views.SisyphusToast;
import com.sisyphus.spinach.ui.configs.IActivityConfigProvider;
import com.sisyphus.simple.R;

import java.util.Arrays;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 用户注册
 */
public class RegisterActivity extends AppCompatActivity implements IActivityConfigProvider {

    @Override
    public int getLayoutResID() {
        return R.layout.activity_register;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button button = findViewById(R.id.button_register);

        button.setOnClickListener(this::register);
    }

    private void register(View button) {
        EditText editEmail = findViewById(R.id.edit_email);
        EditText editNickname = findViewById(R.id.edit_nickname);
        EditText editPassword = findViewById(R.id.edit_password);

        new FormTask.Builder<>()
                .form(new Form.NotEmpty(Arrays.asList(editEmail, editNickname), "不能为空"))
                .view(new SisyphusToast<>(this))
                .netTask(
                        new NetSingleTask<>(
                                getLifecycle(),
                                DataRepository.getInstance()
                                        .register(
                                                editEmail.getText().toString(),
                                                editPassword.getText().toString(),
                                                editNickname.getText().toString()
                                        )
                        )
                )
                .view(new Debounce<>(button, new SisyphusToast<>(RegisterActivity.this)))
                .build()
                .execute();
    }

}
